package com.chixing.day13_list;

import com.chixing.day11_exception.LinkString;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("good");
        list.add("big");
        list.add("world");
        list.add("string");

        list.remove(2);//返回 被删除位置上的元素值
        list.remove("good");//返回 true/false
        //list.clear();
        list.toArray();
        System.out.println(list);

        //遍历
        Iterator<String> it = list.iterator();
        while (it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }


        //LinkedList


        Student student1 = new Student(21,"mark");
        Student student2 = new Student(20,"maliy");
        Student student3 = new Student(24,"katy");
        LinkedList<Student> linkedList = new LinkedList<>();
//       (1)添加2个学生对象
        linkedList.add(student1);
        linkedList.add(student2);
        System.out.println(linkedList);
//       (2) 获得元素个数
        System.out.println(linkedList.size());
//       (3) 添加一个新学生在第一个位置上
        linkedList.addFirst(student3);
        System.out.println(linkedList);
//       (4) 获得名称为"katy" 的学生，对象
        for (int i=0;i<linkedList.size();i++){
            if (linkedList.get(i).getStuName().equals("katy" ))
                System.out.println(linkedList.get(i));
        }
//       (5)迭代遍历集合

        //迭代器 Iterator
        Iterator<Student> it1 = linkedList.iterator();
        while (it1.hasNext()){
            Student student = it1.next();
            System.out.println("student:::"+student);
        }
        //foreach：底层也是迭代器，以上语法简化
        for (Student student:linkedList){
            System.out.println(student);
        }
//       (6) 获得第2个学生
        System.out.println(linkedList.get(1));
//       (7) 删除第1个学生对象
        linkedList.remove(0);
        System.out.println(linkedList);
//       (8)将最后一个学生的年龄改为22
        linkedList.getLast().setStuAge(22);
        //linkedList.set(linkedList.size()-1,student2);
        System.out.println(linkedList);


        System.out.println("--------");
        System.out.println("peek:默认获得第一个元素"+linkedList.peek());
        System.out.println("pop:默认获得第一个元素"+linkedList.pop());






    }
}
class MyArrayList<E>{
    private Object[] defaltArray;

    public E get(){
        return null;
    }
}